<%@ include file="includes/header.jsp" %>

<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map.*" %>
<%@ page import="java.sql.*"%>
	<h1>Afrekenen</h1>
	<fieldset>
		<legend>Inhoud winkelwagen</legend>
		<table>
		<thead>
			<tr>
				<th>Product</th>
				<th>Prijs</th>
				<th></th>
			</tr>
		</thead>
		
		<tbody id="shoppingCartContent">
			<%
				HashMap<Integer, Integer> hm2 = (HashMap)session.getAttribute("mapje");
				if(hm2 != null) {
					for (Entry<Integer, Integer> entry : hm2
							.entrySet()) {
						int productId = entry.getKey();
						int amount = entry.getValue();
						
						// Generate MySQL compatible current date
						java.text.DateFormat dateFormat2 = new java.text.SimpleDateFormat("yyyy-MM-dd");
						java.util.Date date2 = new java.util.Date();
						String sDate2 = dateFormat2.format(date2);

						// Build query
						Class.forName("com.mysql.jdbc.Driver");
						Connection con2=DriverManager.getConnection("jdbc:mysql://127.0.0.1/kroketweb", "kroketweb", "password");
						Statement st2=con2.createStatement();
						ResultSet rs2=st2.executeQuery("SELECT p.product_id AS product_id, p.product_name AS product_name, p.product_stock AS product_stock, q.price_value AS price_value" +
								" FROM product p" +
								" INNER JOIN price q" +
								" ON p.product_id = q.product_id" +
								" WHERE q.price_sDate < '"+sDate2+"'" +
								" AND (q.price_eDate > '"+sDate2+"' OR q.price_eDate IS NULL)" +
								" AND p.product_id = "+productId+";");
						
						// Loop through query
						while(rs2.next()) {
							String name 	= rs2.getString("product_name");
							Float fPrice	= rs2.getFloat("price_value");
							fPrice			= fPrice * amount;
							
							// Build up the price(product price * amount)
							float p 		= (float)Math.pow(10,2);
							fPrice 			= fPrice * p;
							float tmp 		= Math.round(fPrice);
							
							fPrice			= tmp/p;

							String sPrice	= fPrice.toString();
						    String price 	= sPrice.replaceAll("\\.", "\\,");
						    
						    String values[] = price.split(",");
						    if(values[1].length() == 1) {
						    	values[1] += "0";
							}
						    price = values[0]+","+values[1];
						    
						    // Print all rows
							out.println("<tr id=\"item"+productId+"\">" +
											"<td>("+amount+"x) "+name+"</td>" +
											"<td class=\"itemPrice\">"+price+"</td>"+
										"</tr>");
						}
					}
				}
			%>
		</tbody>
		
		<tfoot>
			<tr>
				<td>Totaal</td>
				<td id="totalPriceCart">0,00</td>
			</tr>
		</tfoot>
	</table>
	</fieldset>
	
	<fieldset>
		<legend>Bezorggegevens</legend>
		<form method="post" id="deliveryInformation">
			<table>
				<tbody>
					<tr>
						<td>Naam</td>
						<td><input id="name" type="text" value="" /></td>
					</tr>
					<tr>
						<td>Straat + nr</td>
						<td><input id="address" type="text" value="" /></td>
					</tr>
					<tr>
						<td>Plaats</td>
						<td><input id="city" type="text" value="Groningen" /></td>
					</tr>
					<tr>
						<td>Telefoonnummer</td>
						<td><input id="phoneNumber" type="text" value="" /></td>
					</tr>
					<tr>
						<td></td>
						<td><span id="orderButton" onclick="validateOrderForm();">Bestel</span></td>
					</tr>
				</tbody>
			</table>
		</form>
	</fieldset>
<%@ include file="includes/footerCashOut.jsp" %>